<?php
/**
 * Created by PhpStorm.
 * User: 于婧雯
 * Date: 2019/2/22
 * Time: 10:02
 */
namespace App\Http\Controllers\NewAdmin;

use App\Http\Controllers\AdapterWebController;
use Illuminate\Http\Request;
use App\Modules\UCenter\Myaddr as ApiMyaddr;
use App\Modules\Goods\Goods as ApiGoods;
use App\Modules\UCenter\Integral as ApiIntegral;
//后台商城管理
class Shop extends AdapterWebController
{
    /*---------------------------------------收货地址--------------------------------------------------------------------*/
    // 收货地址列表
    public function adminMyAddrList(Request $request)
    {
        if (empty($request['keywords'])) {
            $postData['keywords'] = '';
        } else {
            $postData['keywords'] = $request['keywords'];
        }
        $list = $this->appLayerExec(ApiMyaddr::class, 'adminMyAddrList', $postData);
        return view('newAdmin.shop.addr_list', ['list' => $list['res']]);
    }

    // 删除收货地址
    public function adminDelMyAddr($id)
    {
        $postData['id'] = (int)$id;
        $res = $this->appLayerExec(ApiMyaddr::class, 'adminDelMyAddr', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '收货地址已删除!'];
        }
        return ['status' => -1, 'msg' => '收货地址删除异常，请重试!'];
    }
    /*---------------------------------------商品管理--------------------------------------------------------------------*/
    // 商品列表
    public function goodsList(Request $request)
    {
        if (empty($request['keywords'])) {
            $postData['keywords'] = '';
        } else {
            $postData['keywords'] = $request['keywords'];
        }
        $list = $this->appLayerExec(ApiGoods::class, 'goodsList', $postData);
        return view('newAdmin.shop.goods_list', ['list' => $list['res']]);
    }
    // 添加商品
    public function addGoods(Request $request)
    {
        if ($request->isMethod('get')) {
            return view('newAdmin.shop.goods_add');
        }
        $postData = $request->all();
        $res = $this->appLayerExec(ApiGoods::class, 'addGoods', $postData);

        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '商品增加成功!'];
        }

        return ['status' => -1, 'msg' => '商品增加失败，请重试!'];
    }
    // 编辑商品
    public function altGoods(Request $request, $id)
    {
        if ($request->isMethod('get')) {
            $postData['id'] = (int)$id;
            $res = $this->appLayerExec(ApiGoods::class, 'goodsInfo', $postData);
            if ($res['code'] == 200) {
                return view('newAdmin.shop.goods_edit', ['info' => $res['res']]);
            }
            return false;
        }
        $postData = $request->all();
        $res = $this->appLayerExec(ApiGoods::class, 'altGoods', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '商品编辑成功!'];
        }
        return ['status' => -1, 'msg' => '商品编辑失败，请重试!'];
    }
    // 删除商品
    public function delGoods($id)
    {
        $postData['id'] = (int)$id;
        $res = $this->appLayerExec(ApiGoods::class, 'delGoods', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '商品删除成功!'];
        }
        return ['status' => -1, 'msg' => '商品删除异常，请重试!'];
    }
    // 文件(图片)上传
    public function upload(Request $request)
    {
        $fileRes = $this->fileUpload($request, 'files');
        if (!$fileRes['status']) {
            return ["code" => 1, "msg" => "图片上传失败！"];
        }
        return ["code" => 0, "msg" => "图片上传成功！", "url" => $fileRes['url']];
    }
    /*---------------------------------------积分 订单管理--------------------------------------------------------------------*/
    // 用户积分列表 可查用户
    public function adminIntegralList(Request $request)
    {
        if (empty($request['keywords'])) {
            $postData['keywords'] = '';
        } else {
            $postData['keywords'] = $request['keywords'];
        }
        $list = $this->appLayerExec(ApiIntegral::class, 'adminIntegralList', $postData);
        return view('newAdmin.shop.integral_list', ['list' => $list['res']]);
    }
    // 积分操作记录 可查用户
    public function adminOptionLog(Request $request)
    {
        if (empty($request['keywords'])) {
            $postData['keywords'] = '';
        } else {
            $postData['keywords'] = $request['keywords'];
        }
        $list = $this->appLayerExec(ApiIntegral::class, 'adminOptionLog', $postData);
        return view('newAdmin.shop.option_log_list', ['list' => $list['res']]);
    }
    // 用户兑换记录列表 可查用户
    public function adminOrderList(Request $request)
    {
        if (empty($request['keywords'])) {
            $postData['keywords'] = '';
        } else {
            $postData['keywords'] = $request['keywords'];
        }
        $list = $this->appLayerExec(ApiIntegral::class, 'adminOrderList', $postData);
        return view('newAdmin.shop.order_list', ['list' => $list['res']]);
    }
    // 发货
    public function deliverGoods($id)
    {
        $postData['id'] = (int)$id;
        $res = $this->appLayerExec(ApiIntegral::class, 'deliverGoods', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '发货成功!'];
        }
        return ['status' => -1, 'msg' => '发货异常，请重试!'];
    }

}
